﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class donviToSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public donviToSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public List<_listJsontoTree> getListDV_Json()
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();
                var donvi = dbContext.DONVI.ToList();
                foreach (var item in donvi)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = item.MA;
                    itemMVT.text = item.TEN;
                    itemMVT.type = "dv";
                    itemMVT.Idparent = item.MA;
                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _list = new List<_listJsontoTree>();
                return _list;
            }
        }

        public List<_listJsontoTree> getListTree(string[] _donvi, string[] toql)
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();

                var _listMVT = dbContext.DONVI.Where(p => _donvi.Contains(p.MA)).ToList();

                foreach (var item in _listMVT)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = item.MA;
                    itemMVT.text = item.TEN;
                    itemMVT.type = "dv";
                    itemMVT.Idparent = item.MA;
                    var listChild = dbContext.TOKT.Where(p => p.DONVI == item.MA  && toql.Contains(p.MA)).ToList();
                    foreach (var itemloaiTb in listChild)
                    {
                        _listJsontoTree itemLoaiTb = new _listJsontoTree();
                        itemLoaiTb.id = itemloaiTb.MA.ToString();
                        itemLoaiTb.text = itemloaiTb.TEN;
                        itemLoaiTb.type = "toql";
                        itemLoaiTb.Idparent = item.MA;
                        itemMVT.children.Add(itemLoaiTb);
                    }
                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }

        public List<_listJsontoTree> getListTreeFindSure(string[] _donvi, string[] toql)
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();

                var _listMVT = dbContext.DONVI.Where(p => _donvi.Contains(p.MA)).ToList();

                foreach (var item in _listMVT)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = "";
                    itemMVT.text = item.TEN;
                    itemMVT.type = "dv";
                    itemMVT.Idparent = item.MA;
                    var listChild = dbContext.TOKT.Where(p => p.DONVI == item.MA && toql.Contains(p.MA)).ToList();
                    foreach (var itemloaiTb in listChild)
                    {
                        _listJsontoTree itemLoaiTb = new _listJsontoTree();
                        itemLoaiTb.id = itemloaiTb.MA.ToString();
                        itemLoaiTb.text = itemloaiTb.TEN;
                        itemLoaiTb.type = "toql";
                        itemLoaiTb.Idparent = item.MA;
                        itemMVT.children.Add(itemLoaiTb);
                    }
                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }

        public List<_listJsontoTree> getListTreeFindSure_all()
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();

                var _listMVT = dbContext.DONVI.ToList();

                foreach (var item in _listMVT)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = item.MA + "-DV";
                    itemMVT.text = item.TEN;
                    itemMVT.type = "dv";
                    itemMVT.Idparent = item.MA;
                    var listChild = dbContext.TOKT.Where(p => p.DONVI == item.MA).ToList();
                    foreach (var itemloaiTb in listChild)
                    {
                        _listJsontoTree itemLoaiTb = new _listJsontoTree();
                        itemLoaiTb.id = itemloaiTb.MA.ToString() + "-TO";
                        itemLoaiTb.text = itemloaiTb.TEN;
                        itemLoaiTb.type = "toql";
                        itemLoaiTb.Idparent = item.MA;
                        itemMVT.children.Add(itemLoaiTb);
                    }
                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }


        public List<_listJsontoTree> getListTreeNotType(string[] _donvi, string[] toql)
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();

                var _listMVT = dbContext.DONVI.Where(p => _donvi.Contains(p.MA)).ToList();

                foreach (var item in _listMVT)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = "";
                    itemMVT.text = item.TEN;
                    itemMVT.type = "dv";
                    itemMVT.Idparent = item.MA;
                    var listChild = dbContext.TOKT.Where(p => p.DONVI == item.MA && toql.Contains(p.MA)).ToList();
                    foreach (var itemloaiTb in listChild)
                    {
                        _listJsontoTree itemLoaiTb = new _listJsontoTree();
                        itemLoaiTb.id = itemloaiTb.MA.ToString();
                        itemLoaiTb.text = itemloaiTb.TEN;
                        itemLoaiTb.type = "toql";
                        itemLoaiTb.Idparent = item.MA;
                        itemMVT.children.Add(itemLoaiTb);
                    }
                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }

        public List<_listJsontoTree> getListTreeAllVnptSite(string[] _donvi, string[] toql, string TramVt)
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();

                var _listMVT = dbContext.DONVI.Where(p => _donvi.Contains(p.MA)).ToList();

                foreach (var item in _listMVT)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = item.MA;
                    itemMVT.text = item.TEN;
                    itemMVT.type = "dv";
                    itemMVT.Idparent = item.MA;
                    itemMVT.Idparent_parent = item.MA;
                    var listChild = dbContext.TOKT.Where(p => p.DONVI == item.MA && toql.Contains(p.MA)).ToList();
                    foreach (var itemloaiTb in listChild)
                    {
                        _listJsontoTree itemLoaiTb = new _listJsontoTree();
                        itemLoaiTb.id = itemloaiTb.MA.ToString();
                        itemLoaiTb.text = itemloaiTb.TEN;
                        itemLoaiTb.type = "toql";
                        itemLoaiTb.Idparent = item.MA;
                        itemLoaiTb.Idparent_parent = itemloaiTb.MA;

                        var listVNPTSITE = (from i in dbContext.VNPT_SITE
                                           join k in dbContext.SITE_MANGVT on i.SITEID equals k.SITEID
                                           join l in dbContext.MANGVT on k.MANGVT equals l.MA
                                            where (l.MA == TramVt || string.IsNullOrEmpty(TramVt) == true) && i.TOQL == itemloaiTb.MA
                                           select i).ToList();

                        if (listVNPTSITE != null)
                        {

                            foreach (var itemloaivnptSite in listVNPTSITE)
                            {
                                _listJsontoTree itemLoaisite = new _listJsontoTree();
                                itemLoaisite.id = itemloaivnptSite.SITEID.ToString();
                                itemLoaisite.text = itemloaivnptSite.TEN;
                                itemLoaisite.type = "vnptsite";
                                itemLoaisite.Idparent = itemloaiTb.MA;
                                itemLoaisite.Idparent_parent = item.MA;
                                //itemMVT.children.Add(itemLoaiTb);
                                itemLoaiTb.children.Add(itemLoaisite);

                            }

                        }
                        itemMVT.children.Add(itemLoaiTb);
                    }

                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }

        public List<_listJsontoTree> listDVTOByTramFromDauNoi(string[] _donvi, string[] toql)
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();

                var _listMVT = dbContext.DONVI.Where(p => _donvi.Contains(p.MA)).ToList();
                                

                foreach (var item in _listMVT)
                {
                    _listJsontoTree itemMVT = new _listJsontoTree();
                    itemMVT.id = item.MA;
                    itemMVT.text = item.TEN;
                    itemMVT.type = "dv";
                    itemMVT.Idparent = item.MA;
                    itemMVT.Idparent_parent = item.MA;
                    var listChild = dbContext.TOKT.Where(p => p.DONVI == item.MA && toql.Contains(p.MA)).ToList();
                    foreach (var itemloaiTb in listChild)
                    {
                        _listJsontoTree itemLoaiTb = new _listJsontoTree();
                        itemLoaiTb.id = itemloaiTb.MA.ToString();
                        itemLoaiTb.text = itemloaiTb.TEN;
                        itemLoaiTb.type = "toql";
                        itemLoaiTb.Idparent = item.MA;
                        itemLoaiTb.Idparent_parent = itemloaiTb.MA;
                        
                        itemMVT.children.Add(itemLoaiTb);
                    }

                    _listRetuen.Add(itemMVT);
                }

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }

        public bool AdDonVi(DONVI entity)
        {
            try
            {
                dbContext.DONVI.Add(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public string UpdateDonVi(DONVI entity)
        {
            dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.MA;
        }

        public DONVI get_DV_ById(string maDV)
        {
            try
            {
                return dbContext.DONVI.Where(p => p.MA == maDV).First();
            }
            catch (Exception e)
            {
                DONVI _ent = new DONVI();
                return _ent;
            }
        }

        public bool DeleteDonVi(string ma)
        {
            try
            {
                var entity_ = get_DV_ById(ma);
                dbContext.DONVI.Remove(entity_);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<DONVI> getListDV()
        {
            try
            {
                return dbContext.DONVI.ToList();
            }
            catch (Exception e)
            {
                List<DONVI> _list = new List<DONVI>();
                return _list;
            }
        }

        public List<TOKT> getListByDonViId(string donviId)
        {
            try
            {
                return dbContext.TOKT.Where(p => (p.DONVI == donviId)).ToList();
            }
            catch (Exception e)
            {
                List<TOKT> _list = new List<TOKT>();
                return _list;
            }
        }

        public List<TOKT> getListTO(string donvi, string[] toqlMe)
        {
            try
            {
                return dbContext.TOKT.Where(p => (p.DONVI == donvi) && toqlMe.Contains(p.MA)).ToList();
            }
            catch (Exception e)
            {
                List<TOKT> _list = new List<TOKT>();
                return _list;
            }
        }

        public TOKT get_To_ById(string maTO)
        {
            try
            {
                return dbContext.TOKT.Where(p => p.MA == maTO).First();
            }
            catch (Exception e)
            {
                TOKT _ent = new TOKT();
                return _ent;
            }
        }

        public bool DeleteToQL(string ma)
        {
            try
            {
                var entity_ = get_To_ById(ma);
                dbContext.TOKT.Remove(entity_);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public string[] ListArayDVByUser(decimal userid)
        {
            try
            {
                return dbContext.USER_DONVI.Where(p => p.USERID == userid).Select(p => p.DONVIID).ToArray();

            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string[] ListArayTOByUser(decimal userid)
        {
            try
            {
                return dbContext.USER_DONVI.Where(p => p.USERID == userid).Select(p => p.TOID).ToArray();

            }
            catch (Exception e)
            {
                return null;
            }
        }

        public bool AdTOKT(TOKT entity)
        {
            try
            {
                dbContext.TOKT.Add(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public string UpdateTOKT(TOKT entity)
        {
            dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.MA;
        }

        public bool addorUpdateTO_PHUONG(TO_PHUONGXA entity)
        {
            try
            {
                if (entity.ID == 0) dbContext.TO_PHUONGXA.Add(entity);
                else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }


    }
}
